package interfaces.spawnMenu.costumize.items;

import java.util.List;

import org.fenggui.binding.render.ImageFont;
import org.fenggui.event.ButtonPressedEvent;
import org.fenggui.event.IButtonPressedListener;

import fileHandling.language.Lang;

import logic.common.player.Player;
import logic.items.Buyable;
import logic.ships.hunter.Hunter;
import interfaces.GUISource;
import interfaces.language.LanguageButton;
import interfaces.superWidgets.StaticContent;
import interfaces.tables.ButtonTable;

public abstract class BuyableContent<T extends Buyable> extends StaticContent {
	
	protected Hunter hunter;
	protected ButtonTable<BuyableButton<T>> itemTable;
	protected BuyableButton<T> selectedItemButton;
	protected LanguageButton buyButton, sellButton;
	protected int tableWidth, tableHeight, buttonHeight;
	protected BuyableInfoContent<T> infoContent;
	protected ImageFont buttonFont;
	
	public BuyableContent(int width, int height, Hunter hunter) {
		super(width, height);
		this.hunter = hunter;

		buttonFont = GUISource.middleFont;
		buttonHeight = buttonFont.getHeight() / 4 * 5;
		
		makeItemTable();
		makeButtons();
		
		layout();
	}
	
	protected abstract List<T> getBuyableList();
	
	protected abstract int getBoughtAmount(T settings);
	
	protected abstract void buyItem(T settings);
	
	protected abstract void sellItem(T settings);
	
	protected abstract Lang getBuyButtonText();
	
	protected abstract Lang getSellButtonText();
	
	protected void makeItemTable() {
		tableWidth = width / 15 * 4;
		tableHeight = height;
		
		itemTable = new ButtonTable<BuyableButton<T>>(tableWidth, tableHeight);
		addWidget(itemTable);

		int infoX = tableWidth + (height / 10);
		int infoWidth = width - infoX;
		int infoHeight = height - buttonHeight;
		int infoY = 0;
		
		infoContent = new BuyableInfoContent<T>(infoWidth, infoHeight);
		infoContent.setXY(infoX, infoY);
		addWidget(infoContent);
		
		List<T> properties = getBuyableList();
		
		for(T props : properties) {
			addItem(props);
		}
	}
	
	protected void addItem(T props) {
		final BuyableButton<T> button = new BuyableButton<T>(props, GUISource.middleFont);
		button.updateMoney(hunter.getPilot().getMoney());
		button.addButtonPressedListener(new IButtonPressedListener() {
			@Override
			public void buttonPressed(ButtonPressedEvent e) {
				setSelectedItemButton(button);
			}
		});
		itemTable.addButton(button);
	}
	
	protected void setSelectedItemButton(BuyableButton<T> button) {
		selectedItemButton = button;
		T props = selectedItemButton.getProperties();
		int amount = getBoughtAmount(props);
		infoContent.setItemType(props, amount);
	}
	
	protected void makeButtons() {
		int buttonWidth = (width - tableWidth) / 2;
		int buttonX = tableWidth;
		int buttonY = height - buttonHeight;
		
		buyButton = new LanguageButton(getBuyButtonText(), buttonFont);
		buyButton.setSize(buttonWidth, buttonHeight);
		buyButton.setXY(buttonX, buttonY);
		buyButton.addButtonPressedListener(new IButtonPressedListener() {
			@Override
			public void buttonPressed(ButtonPressedEvent e) {
				if(selectedItemButton != null) {
					T props = selectedItemButton.getProperties();
					Player player = hunter.getPilot();
					if(props.getCosts() <= player.getMoney()) {
						player.spendMoney(props.getCosts());
						buyItem(props);
						infoContent.updateAmount(getBoughtAmount(props));
					}
				}
			}
		});
		addWidget(buyButton);
		buttonX += buttonWidth;
		
		sellButton = new LanguageButton(getSellButtonText(), GUISource.middleFont);
		sellButton.setSize(buttonWidth, buttonHeight);
		sellButton.setXY(buttonX, buttonY);
		sellButton.addButtonPressedListener(new IButtonPressedListener() {
			@Override
			public void buttonPressed(ButtonPressedEvent e) {
				if(selectedItemButton != null) {
					T props = selectedItemButton.getProperties();
					if(getBoughtAmount(props) == 0) return;
					
					hunter.getPilot().addMoney(props.getCosts());
					sellItem(props);
					infoContent.updateAmount(getBoughtAmount(props));
				}
			}
		});
		addWidget(sellButton);
	}
	
	public void updateMoney(int newMoney) {
		for(BuyableButton<T> button : itemTable.getButtons()) {
			button.updateMoney(newMoney);
		}
	}
}